﻿using LuShanTea.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LuShanTea.DTO;
using LuShanTea.Service.Entities;
using System.Data.Entity;

namespace LuShanTea.Service
{
    public class AreaService : IAreaService
    {
       
        private AreaDTO ToDTO(Area en)
        {
            AreaDTO dto = new AreaDTO();
            dto.CityId = en.CityId;
            dto.CityName = en.City.Name;
            dto.Id = en.Id;
            dto.CreateDateTime = en.CreateDateTime;
            dto.Name = en.Name;
            return dto;
        }

        public long AddNew(string name, long cityId)
        {
            
            Area en = new Area();
            en.Name = name;
            en.CityId = cityId;
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Area> bs = new BaseService<Area>(ctx);
                var res = bs.GetAll().SingleOrDefault(e => e.Name == name && e.CityId == cityId);
                if (res != null)
                {
                    throw new ArgumentException("该区域已存在");
                }
                ctx.Areas.Add(en);
                ctx.SaveChanges();
                return en.Id;
            }
        }

        public AreaDTO[] GetAll()
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Area> bs = new BaseService<Area>(ctx);
                return bs.GetAll()
                    .AsNoTracking()
                    .Include(e => e.City)
                    .ToList().Select(e => ToDTO(e)).ToArray();
            }
        }

        public AreaDTO[] GetByCityId(long cityId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<City> cityBs = new BaseService<City>(ctx);
                var city = cityBs.GetById(cityId);
                if (city == null)
                {
                    throw new ArgumentException("该城市不存在");
                }
                BaseService<Area> areaBs = new BaseService<Area>(ctx);
                return areaBs.GetAll().Where(e => e.CityId == cityId)
                      .AsNoTracking()
                      .Include(e => e.City)
                      .ToList().Select(e => ToDTO(e)).ToArray();
            }
        }

        public AreaDTO GetById(long areaId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Area> bs = new BaseService<Area>(ctx);
                var res = bs.GetAll().Include(a => a.City).SingleOrDefault(a => a.Id == areaId);
                return res == null ? null : ToDTO(res);
            }
        }

        public void MarkDelete(long areaId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Area> bs = new BaseService<Area>(ctx);
                bs.MakeDelete(areaId);
            }
        }

        public void Update(long areaId, string name, long cityId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Area> bs = new BaseService<Area>(ctx);
                var en = bs.GetById(areaId);
                if (en == null)
                {
                    throw new ArgumentException("该区域不存在");
                }

                //TODO:检查数据是否重复

                en.CityId = cityId;
                en.Name = name;
                ctx.SaveChanges();
            }
        }

        public AreaDTO GetByName(string name)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Area> bs = new BaseService<Area>(ctx);
                var res = bs.GetAll().SingleOrDefault(e => e.Name == name);
                return res == null ? null : ToDTO(res);
            }
        }
    }
}
